package com.trisan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Field;
import java.util.*;

@Component
public class EmpDAO {
	

	@Autowired
	HibernateTemplate htTemplate;
	
	// sending insert query to database 
	public boolean save(Emp e){
		boolean status=false;
		if(e.id==htTemplate.save(e)){
			status=true;
		}
		return status;
	}
	
	//sending update query to database
	public boolean update(Emp e){
		boolean status=false;
			try{
				htTemplate.update(e);
				status=true;
			}
			catch(Exception he){
				return status;
			}
		
		return status;
	}
	
	//sending delete query to database
	public boolean delete(Emp e){
		boolean status=false;
		try{
			htTemplate.delete(e);
			status=true;
		}
		catch(Exception e1){
			return status;
		}
		return status;
	}
	
	//sending retrieve query to database for single instance
	
	public Emp getEmp(int id){
		Emp e1;
		e1=htTemplate.get(Emp.class, id);
		return e1;
	}

	//sending retrieve query to database for single instance
	
	public List<Emp> getEmps(){
		List<Emp> al;
			al=htTemplate.find("from com.trisan.Emp");
		return al;
	}
	
	public List<Emp> getEmpNames(String st) {
		String qey="from com.trisan.Emp e where e.ename like '"+st+"%'";
	
		List<Emp> ls=htTemplate.find(qey);
	   
		//List<String> s=new ArrayList<String>();
		
		//for(Emp o1:ls){
		//	Object[] o2=(Object[])o1;
				
//			Integer i1=(Integer)o2[0];
	//			System.out.println("number "+i1);
		//		e.setId(i1);
			//	String s4=(String)o2[1];
			//	e.setEname(s4);
			//	String s5=(String)o2[2];
			//	e.setEmail(s5);
			//	Integer i2=(Integer)o2[3];
			//	e.setSalary(i2);
			//	String s6=(String)o2[4];
			//	e.setDeptid(s6);
			//	s.add(o1.getEname());

			//	System.out.println("String "+o1.getEname());
			//}
		//	System.out.println("ha "+s);
			/**Object [] s1=(Object[])o1;//????
			for(Object o2:s1){
				Integer s2=(Integer)o2;
				System.out.println(s2);
			}*/
				
		return ls;
	}

}
